package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书");
        // 展示图书
        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            System.out.println(book);
        }

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的图书名称");
        String name = scanner.nextLine();

        // 查到该书是否存在
        for (int i = 0; i < currentSize; i++) {
            if (bookList.getBook(i).getName().equals(name)) {
                for (int j = i; j < currentSize - 1; j++) {
                    // [j] = [j + 1]
                    bookList.setBook(j, bookList.getBook(j + 1));
                }
                bookList.setBook(currentSize - 1, null);
                bookList.setUsedSize(currentSize - 1); // 删除后，当前数量减一
                System.out.println("删除成功");
                return;
            }
        }
        System.out.println("删除失败，该书不存在或者已经被删除");
    }
}
